import { Component,ViewChild} from '@angular/core';
import { NavController,Slides,ToastController,LoadingController} from 'ionic-angular';

import {AppContentService} from "../../providers/AppContentService";
import {BANNER_PATH} from "../../providers/Constants"
import {BaseUI} from "../../common/baseui";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage extends BaseUI {
	@ViewChild(Slides) slides: Slides;
  users: Array<{
    userface: string,
    username:string,
    pubtime:any,
    img:any,
    component: any,
    likeNum:any,
    commetNum:any,
    ago:any,
  }>;
  errorMessage: any;
  banners: string[];
  bannerPath:any;
  constructor(
              public navCtrl: NavController,
              private appContService : AppContentService,
              public toastCtrl:ToastController,
              public loadingCtrl:LoadingController,
  ) {
    super();
    this.users = [
      { userface: '2.jpg', username: '张小微',pubtime:'2017-7-21',img:'b1.jpg',component:'什么情人节快乐祝福语听多了，红包最实在',likeNum:13,commetNum:30,ago:11},
      { userface: '3.jpg', username: '张小微',pubtime:'2017-7-21',img:'b2.jpg',component:'你说你不喜欢月亮,你说你喜欢星星,可我喜欢月亮呀,因为今晚的月色真美！',likeNum:13,commetNum:30,ago:11},
    ];
    this.bannerPath = BANNER_PATH;
  }

  // ngOnInit(){//页面加载完成后自己调用
  //   this.slides.pager = true;
  //   //参照官网这个属性表示轮播图能够循环
  //   this.slides.loop = true;
  //   setInterval(()=>{
  //       //1000:表示速度，true表示能够触发事件
  //       this.slides.slideNext(1000,true);
  //   },2000);
  // }
//解决自动播放的Slides调转到其他页面一定时间后（超过设置的播放时间），再回到此页面，自动播放失效
   //页面进入时启动自动播放
  // 进入的时候自动播放
  // ionViewWillEnter() {
  //   this.bannerPath = BANNER_PATH;
  //   this.getBannerList();
  //   this.slides.startAutoplay();
  //   console.log("页面载入");
  // }



  ionViewDidEnter(){

      this.getBannerList();
      if(this.slides){
        this.slides.startAutoplay();
      }



  }
// 离开的时候暂停

  ionViewDidLeave() {
    if(this.slides){
      this.slides.stopAutoplay();
    }

    console.log("页面退出");
  }

  //请求服务器资源
  getBannerList(){
     this.appContService.getBannerList().subscribe(
       res=>{
         console.log(res["msg"]);
         this.banners = res["data"];
       }, error => {
         this.errorMessage = error;
         // this.toastCtrl.create({message:"网络请求失败", duration: 4500,position:'middle'}).present();
         super.showToast(this.toastCtrl,"网络请求失败",'middle');
       });
  }

  //刷新
  doRefresh(refresher) {
    setTimeout(() => {
      this.getBannerList();
      refresher.complete();
    }, 2000);
  }

  //打开发布动态页面
  openPubSportDynamic(){
    this.navCtrl.push('PubSportDynamicPage');
  }



}
